<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/8/2
 * Time: 14:45
 */

namespace app\api\controller\information;

use app\api\controller\BasicApi;
use think\Db;

class Index extends BasicApi
{
    /**
     * 获取资讯首页推荐文章
     * @return \think\response\Json
     */
    public function recommend_article()
    {
        try{
            //查询全部信息分类
            $cate_whr=[
                ['id','=',5],
                ['pid','=',4],
                ['status','=',1],
                ['id','<>',23]
            ];
            $cates=app('systemCateService')::get_cate_list($cate_whr);
            foreach ($cates as &$cate) {
                $cate['arts']=Db::name('article')->where('status',1)->where('type',$cate['id'])
                    ->order('sort,id desc')->field('id,title,FROM_UNIXTIME(create_at,\'%Y-%m-%d\') create_at,borwse')->cache(60)->limit(5)->select();
            }
            return json(['msg'=>'success','data'=>$cates]);
        }catch(\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

    /**
     * 信息咨询通栏广告
     * @return \think\response\Json
     */
    function ad(){
        try{
            $ad=Db::name('system_ad')->where('status',1)->where('id',10)
                ->cache(60)->field('id,image,url')->find();
            return json(['msg'=>'success','data'=>$ad]);
        }catch(\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

    /**
     * 获取资讯首页快讯10条
     * @return \think\response\Json
     */
    public function new_flash()
    {
        try{
            //查询10条最新文章
            $list=Db::name('article')->where('status',1)->order('sort,id desc')->field('id,title')->cache(60)->limit(10)->select();
            return json(['msg'=>'success','data'=>$list]);
        }catch(\Exception  $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

    /**
     * 获取文章列表
     * @return \think\response\Json
     */
    public function article_list(){
        try{
            $page_size=input('page_size',10);
            $page_now=input('page_now',1);
            $type=input('type');
            $is_recommend=input('is_recommend',0);
            $is_video=input('is_video',0);
            $db=Db::name('article')->where('status',1)
                ->order('sort,id desc')->field('id,title,FROM_UNIXTIME(create_at,\'%Y-%m-%d\') create_at,borwse,images,mid,video_url')->cache(60);
            if($type){
                $db->where('type',$type);
            }
            if($is_recommend){
                $db->whereRaw('FIND_IN_SET(1,flag)');
            }
            if($is_video){
                $db->where('video_url','<>','');
            }
            $list=$db->page($page_now,$page_size)->select();
            foreach ($list as &$item) {
                $item['images'] = $item['images']?explode('|',$item['images']):[];
                $item['praised'] = Db::name('praise')->where('attach_id',$item['id'])->where('type',3)->count();
                $item['member_info']=!$item['mid']?'官方发布':Db::name('store_member')->where('id',$item['mid'])->value('nickname');
            }
            return json(['msg'=>'success','data'=>$list]);
        }catch (\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }


    /**
     * 获取案例列表
     * @return \think\response\Json
     */
    public function case_list(){
        try{
            $page_size=input('page_size',10);
            $page_now=input('page_now',1);
            $is_recommend=input('is_recommend',0);
            $province=input('province');
            $db=Db::name('member_case')->where('status',1)
                ->order('id desc')->field('id,title,FROM_UNIXTIME(addtime,\'%Y-%m-%d\') create_at,borwse,images,mid')->cache(60);
            if($is_recommend){
                $db->where('FIND_IN_SET(1,flag)');
            }
            if($province){
                $db->where('province',$province);
            }
            $list=$db->page($page_now,$page_size)->select();
            foreach ($list as &$item) {
                $item['images']?$item['images'] = explode('|',$item['images']):'';
                $item['praised'] = Db::name('praise')->where('attach_id',$item['id'])->where('type',1)->count();
                $item['member_info']=!$item['mid']?'官方发布':Db::name('store_member')->where('id',$item['mid'])->value('nickname');
            }
            return json(['msg'=>'success','data'=>$list]);
        }catch (\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }
}